package cn.com.niit.controller;

import cn.com.niit.pojo.Menu;
import cn.com.niit.pojo.Merchant;
import cn.com.niit.pojo.User;
import cn.com.niit.service.BillService;
import cn.com.niit.service.MenuService;
import cn.com.niit.service.MerchantService;
import cn.com.niit.service.UserService;

import org.springframework.beans.factory.annotation.Autowired;

import org.springframework.web.bind.annotation.*;


import javax.servlet.http.HttpSession;
import java.util.List;

@RestController
@CrossOrigin(origins = "*")
public class UserController {

    @Autowired
    UserService service;
    @Autowired
    BillService billService;
    @Autowired
    MerchantService merchantService;
    @Autowired
    MenuService menuService;

    @RequestMapping("/queryCount")
    public User queryCount(String name){
        return service.queryCount(name);
    }
    @RequestMapping( "/showAllUser")
    public List<User> showAll(User user){
        return service.showAll(user);
    }
    @RequestMapping("/addM")
    public String addM(String userName, String total, HttpSession session){
        User user= service.queryCount(userName);
        Double re=Double.parseDouble(user.getAccount())+Double.parseDouble(total);
        service.addM(re,userName);
        User user1= service.queryCount(userName);
        session.setAttribute("USER",user1);
        return "充值成功！！！";
    }
    @GetMapping("/submit")
    public String Submit(User user) {
        user.setPermission("用户");
        user.setAccount("0");
        service.insertUser(user);
        return "login";
    }
    @RequestMapping( "/pass")
    public void pass(String user_name){
        merchantService.pass(user_name);
        service.pass(user_name);
    }
    @RequestMapping("/pay")
    public String pay(String shopUserName,String userName,String total,HttpSession session) {
        User user = service.queryCount(userName);
        User shop = service.queryCount(shopUserName);

        if (Double.parseDouble(user.getAccount()) >= Double.parseDouble(total)) {
            billService.update(shopUserName, userName);
            Double r1=Double.parseDouble(user.getAccount())-Double.parseDouble(total);
            Double r2=Double.parseDouble(shop.getAccount())+Double.parseDouble(total);
            service.add(r1,userName);
            service.shop(r2,shopUserName);
            User USER = service.queryCount(userName);
            session.setAttribute("USER",USER);
        }else {
            return "余额不足，请充值！！！";
        }
        return "订单支付完成！！！";
    }
    @GetMapping("/logout")
    public String out(HttpSession session){
        session.removeAttribute("USER");
        return "index";
    }
    @GetMapping(value = "/login")
    public String login(User user) {
        User user1 = service.queryCount(user.getUname());
        if (user1== null) {
            return "submit";
        }
        if (user.getUname().equals(user1.getUname()) && user.getPwd().equals(user1.getPwd())) {
            switch (user1.getPermission()) {
                case "管理员":
                    return "admin";

                case "用户":
                    return "index";

                case "商家":
                    Merchant shop = merchantService.showOne(user.getUname());
                    if (shop.getFlag().equals("0")) {return "baned";}else{

                        return "shop";
                    }

                default:return "login";
            }
        }
        else {
            return "login";
        }

    }
    @GetMapping("/addMenu")
    public String addMenu(Menu menu, String uname){
        Merchant merchant= merchantService.showOne(uname);
        menu.setMerchant_name(merchant.getUserName());
        menu.setShop_name(merchant.getShopName());
        System.out.println(menu);
        menuService.addMenu(menu);
        return "shop";
    }
}
